﻿using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;

namespace ldhWebApiWF
{
    public class MultipartFormStreamProvider : MultipartFormDataStreamProvider
    {
        public MultipartFormStreamProvider(string path)
            : base(path)
        {
        }

        public override string GetLocalFileName(HttpContentHeaders headers)
        {
            return "BodyPart_" + Guid.NewGuid().ToString() + ".cac";
        }

        public override Stream GetStream(HttpContent parent, HttpContentHeaders headers)
        {
            try
            {
                var fileExtension = Path.GetExtension(Extens.TakeFileName(headers.ContentDisposition.FileName)).ToLower();
                if (fileExtension == ".zip" || fileExtension == ".jpg" || fileExtension == ".pdf" || fileExtension == ".iso")
                {
                    return base.GetStream(parent, headers);
                }
            }
            catch { }
            return Stream.Null;
        }
    }
}
